<?php
namespace app\admin\controller;

use \think\facade\Db;
use app\middleware\JWTAUTH;
use app\admin\model\accounttype;
use app\admin\model\Procount;
use app\admin\model\Proplan;
use app\admin\model\Url;
use app\admin\model\Blacklist;
use think\exception\ValidateException;

class Procount
{
    protected $middleware=['JWTAUTH'];

    public function create(){
      if($this->request->action()=='post'){
          $blacklist_province='';
          $blacklist_country='';
          $blacklist_city='';
          $blacklist_ip='';
          $procount_own_user_id=$token_user_id; //从token中取出

          foreach($this->request->param('blacklist_province') as $bp){
              $blacklist_province.=','.$bp;
          }

          foreach($this->request->param('blacklist_country') as $bc){
              $blacklist_country.=','.$bc;
          }

          foreach($this->request->param('blacklist_city') as $bct){
              $blacklist_city.=','.$bct;
          }

          foreach($this->request->param('blacklist_ip') as $bi){
              $blacklist_ip.=','.$bi;
          }

          try{
            $pro_acount_data=array(
                'name' => $this->request->param('name'),
                'url'  => $this->request->param('url'),
                'token' => $this->request->param('token'),
                'owned_user_id' => $procount_own_user_id,
                'switch' => $this->request->param('switch'),
                'description' => $this->request->param('description'),
                'blacklist_country' => $blacklist_country,
                'blacklist_province' => $blacklist_province,
                'blacklist_city' => $blacklist_city,
                'blacklist_ip' => $blacklist_ip,
            );

            $this->validate(app\validate\Procount::class)
                  ->scene('create')
                  ->check($pro_acount_data);
            $procount=new Procount;
            $procount->save($pro_acount_data);

          }catch(ValidateException $e){
              throw($e);
          }
      }else{
          return json_encode(array(
              'status' => '103',
              'message' => '方法不支持，请用post',
          ));
      }
    }

    public function edit()
    {
      if($this->request->action()=='post'){
          $blacklist_province='';
          $blacklist_country='';
          $blacklist_city='';
          $blacklist_ip='';
          $procount_own_user_id=$token_user_id; //从token中取出

          foreach($this->request->param('blacklist_province') as $bp){
              $blacklist_province.=','.$bp;
          }

          foreach($this->request->param('blacklist_country') as $bc){
              $blacklist_country.=','.$bc;
          }

          foreach($this->request->param('blacklist_city') as $bct){
              $blacklist_city.=','.$bct;
          }

          foreach($this->request->param('blacklist_ip') as $bi){
              $blacklist_ip.=','.$bi;
          }

          try{
            $id=$this->request->param('id');
            $pro_acount_data=array(
                'name' => $this->request->param('name'),
                'url'  => $this->request->param('url'),
                'token' => $this->request->param('token'),
                'switch' => $this->request->param('switch'),
                'description' => $this->request->param('description'),
                'blacklist_country' => $blacklist_country,
                'blacklist_province' => $blacklist_province,
                'blacklist_city' => $blacklist_city,
                'blacklist_ip' => $blacklist_ip,
            );

            $this->validate(app\validate\Procount::class)
                  ->scene('edit')
                  ->check($pro_acount_data);
            $procount=Procount::find(array('id'=>$id,'owned_user_id' => $procount_own_user_id));

            if(empty($procount)){
                return json_encode(array(
                    'status' => '201',
                    'message' => '没有该推广账户',
                ));
            }
            $procount->save($pro_acount_data);

          }catch(ValidateException $e){
              throw($e);
          }
      }else {
          return json_encode(
            array(
              'status' => '103',
              'message' =>'方法错误',
            )
          );
      }
    }


    public function delete(){
        $procount_id=$this->request->param('id');
        $procount_own_user_id=$token_user_id; //从token中取出
        try{
            $this->validate(app\validate\Procount::class)
                ->scene('delete')
                ->check($procount_id);

            $procount=Procount::find(array(
                'id' => $procount_id,
                'owned_user_id' => $procount_own_user_id,
            ));

            if($procount->delete()){
                return json_encode(array(
                    'status' => '100',
                    'message' => '删除成功',
                ));
            }else{
                return json_encode(array(
                    'status' => '203',
                    'message' => '删除失败，请联系管理员查看原因',
                ));
            }
        }catch(ValidateException $e){
            throw($e);
        }
    }

    public findbyname(){
        $procount_name=$this->request->param('name');
        $procount_own_user_id=$token_user_id; //从token中取出
        try{
            $this->validate(app\validate\Procount::class)
                ->scene('findbyname')
                ->check($procount_name);

            $procount=Procount::find(array(
                'name' => $procount_name,
                'owned_user_id' => $procount_own_user_id,
            ));

            if(!empty($procount)){
                return json_encode($procount);
            }else{
                return json_encode(array(
                    'status' => '303',
                    'message' => '未查找到数据',
                ));
            }
        }catch(ValidateException $e){
            throw($e);
        }
    }
}
